package com.proyecto.track.action;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.proyecto.track.bo.Project;
import com.proyecto.track.bo.ProjectStatus;
import com.proyecto.track.bo.UserPerson;
import com.proyecto.track.service.ProjectService;
import com.proyecto.track.service.UserPersonService;


public class ProjectAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	
	private Long idUserPerson;
	private ProjectService projectService;
	@Autowired
	private UserPersonService userPersonService;
	private Project project = new Project();
	private ProjectStatus projectStatus = new ProjectStatus();
	private List<ProjectStatus> projectsStatus = new ArrayList<ProjectStatus>();
	private List<UserPerson> usersInsideProject = new ArrayList<UserPerson>();
	private List<UserPerson> usersOutsidePoject = new ArrayList<UserPerson>();
	private Long[] newUsersInsideProject; 
	private Long[] newUsersOutsideProject;
	private String msg;
	private long id;
	private long idProyecto;

	public ProjectStatus getProjectStatus() {
		return projectStatus;
	}

	public void setProjectStatus(ProjectStatus projectStatus) {
		this.projectStatus = projectStatus;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public List<ProjectStatus> getProjectsStatus() {
		return projectsStatus;
	}

	public void setProjectsStatus(List<ProjectStatus> projectsStatus) {
		this.projectsStatus = projectsStatus;
	}

	public ProjectService getProjectService() {
		return projectService;
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}
	
	public String createProject(){
		System.out.println("SE VA A CREAR EL PROYECTO: " + project.getName());
		System.out.println("EL ID DE LA PERSONA ES: "+ idUserPerson);
		UserPerson userPerson = userPersonService.searchUserPerson(idUserPerson);
		project.setLeader(userPerson);
		id = projectService.createProject(project);
		msg = "Se ha creado un proyecto nuevo id: " + id;
		return "SUCCESS";
	}
	
	public String deleteProject(){
		projectService.deleteProject(id);
		return "SUCCESS";
	}
	
	public String modifyProject(){
		System.out.println("busco el projecto para modificarlo");
		project = projectService.searchProjects(id);
		loadLists();
		return "SUCCESS";
	}
	
	public String testMethod(){
		System.out.println("TESTING ACTION EVENT");
		return "SUCCESS";
	}
	
	public String loadLists(){
		System.out.println("Cargo las listas");
		projectsStatus = projectService.searchProjectStatus();
		return "SUCCESS";
	}
	
	
	public String viewProject(){
		System.out.println("VA A VER EL PROYECTO: " + id);
		project = projectService.searchProjects(id);
		return "SUCCESS";
	}
	
	public String newProjectStatus(){
		projectService.newProjectStatus(projectStatus);
		return "SUCCESS";
	}
	
	public String loadUsersProjectList(){
		usersInsideProject = projectService.getUsersActive(this.id);
		usersOutsidePoject = userPersonService.getUsersFiltered(usersInsideProject);
		idProyecto = id;
		return "SUCCESS";
	}
	
	public String saveUsersProject(){
		projectService.modify(this.idProyecto, userPersonService.searchUsersPerson(newUsersInsideProject));
		this.id=idProyecto;
		project = projectService.searchProjects(idProyecto);
		return "SUCCESS";
	}

	public Long getIdUserPerson() {
		return idUserPerson;
	}

	public void setIdUserPerson(Long idUserPerson) {
		this.idUserPerson = idUserPerson;
	}

	public List<UserPerson> getUsersInsideProject() {
		return usersInsideProject;
	}

	public void setUsersInsideProject(List<UserPerson> usersInsideProject) {
		this.usersInsideProject = usersInsideProject;
	}

	public List<UserPerson> getUsersOutsidePoject() {
		return usersOutsidePoject;
	}

	public void setUsersOutsidePoject(List<UserPerson> usersOutsidePoject) {
		this.usersOutsidePoject = usersOutsidePoject;
	}

	public Long[] getNewUsersInsideProject() {
		return newUsersInsideProject;
	}

	public void setNewUsersInsideProject(Long[] newUsersInsideProject) {
		this.newUsersInsideProject = newUsersInsideProject;
	}

	public Long[] getNewUsersOutsideProject() {
		return newUsersOutsideProject;
	}

	public void setNewUsersOutsideProject(Long[] newUsersOutsideProject) {
		this.newUsersOutsideProject = newUsersOutsideProject;
	}

	public long getIdProyecto() {
		return idProyecto;
	}

	public void setIdProyecto(long idProyecto) {
		this.idProyecto = idProyecto;
	}

}
